﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace BanHang
{
    public partial class XemHoaDon : Form
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ToString());
        SqlDataAdapter dap;
        DataSet ds;
        
        public XemHoaDon()
        {
            InitializeComponent();
        }

        private void loadGVHD()
        {
            string sql = "select MaHoaDon,NgayTao,TenKhachHang,TongTien from HoaDon, KhachHang where HoaDon.MaKhachHang=KhachHang.MaKhachHang";
            dap = new SqlDataAdapter(sql,conn);
            ds = new DataSet();
            dap.Fill(ds,"HoaDon");

            dataGridView2.DataSource = ds.Tables["HoaDon"];
        }

        
        private void XemHoaDon_Load(object sender, EventArgs e)
        {
            loadGVHD();
             
        }

        
        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 5)
            {
                MessageBox.Show(dataGridView2.Rows[e.RowIndex].Cells[1].Value.ToString());
            }
        }

        private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            loadCTGH();
             
            
        }

        private void loadCTGH()
        {
            string maHD;
            maHD = Convert.ToString(dataGridView2.CurrentRow.Cells[0].Value.ToString());

            string sql = "select MaChiTietHoaDon,TenSanPHam,ChiTietHoaDon.Gia as Gia,ChiTietHoaDon.SoLuong as SoLuong from ChiTietHoaDon, SanPham where ChiTietHoaDon.MaSanPham = SanPham.MaSanPham and MaHoaDon = '" + maHD + "'";
            dap = new SqlDataAdapter(sql, conn);
            DataTable dt = new DataTable();
            dap.Fill(dt);

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dataGridView1.Rows.Add(i, dt.Rows[i].ItemArray[0].ToString(), dt.Rows[i].ItemArray[1].ToString(), dt.Rows[i].ItemArray[2].ToString(), dt.Rows[i].ItemArray[3].ToString(), Convert.ToString(double.Parse(dt.Rows[i].ItemArray[2].ToString()) * double.Parse(dt.Rows[i].ItemArray[3].ToString())));
            }
        }
    }
}
